## R Script Outputs ------------------------------------------------------------
# Figure 6: Trends in H-1B Denial Rates Grouped by Treatment Status, 1992--2017 
# Appendix Figure D.1: Trends in Total H-1B Petitions Grouped by Treatment Status, 1992--2017


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra", 
         "viridis")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/JOP-h1b-replication"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "data-merge-91-17-orbis-excluded.RData", sep = "/"))


## Figure 6 --------------------------------------------------------------------
# aggregate
df.a.sum <- data.merge %>%
  group_by(lob_img_2017, year) %>%
  summarize(ave_h1b_deny_rate = mean(h1b_deny_rate, na.rm = TRUE)) %>%
  filter(year >= 1992)

# set parameters
axis.title.size <- 16
colors <- c("black", "black")

p.ave.denial.h1b <- ggplot(df.a.sum,
                           aes(x = year, 
                               y = ave_h1b_deny_rate,
                               group = factor(lob_img_2017),
                               color = factor(lob_img_2017),
                               shape = factor(lob_img_2017))) +
  geom_line(aes(linetype = factor(lob_img_2017), 
                color = factor(lob_img_2017))) +
  scale_linetype_manual(values = c("dashed", "solid")) +
  scale_color_manual(values = colors) +
  geom_vline(xintercept = 2016.5, 
             color = "red") +
  geom_point(size = 4) +
  scale_shape_manual(values = c(17, 19))+
  scale_x_continuous("Year",
                     breaks = seq(1992, 2017, by = 1), 
                     labels = seq(1992, 2017, by = 1),
                     limits = c(1992, 2019)) + 
  scale_y_continuous("Average H-1B Denial Rates",
                     breaks = seq(0, 0.15, by = 0.05),
                     limits = c(0, 0.15)) +
  ggtitle("H-1B Denial Rates") +
  theme_bw()  +
  theme(plot.title = element_text(size = axis.title.size + 6,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 6,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 6,
                                    margin = margin(20, 0, 0, 0)),
        axis.text.x = element_text(size = axis.title.size - 3,
                                   angle = 0),
        axis.text.y = element_text(size = axis.title.size),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none") + 
  annotate("text", 
           x = 2018.6, 
           y = df.a.sum %>% 
             filter(lob_img_2017 == 1 & year == 2017) %>%
             pull(ave_h1b_deny_rate),
           label = "Lobbied\nImmigration\nin 2017", 
           color = "black",
           size = 5.5) + 
  annotate("text", 
           x = 2018.6, 
           y = df.a.sum %>% 
             filter(lob_img_2017 == 0 & year == 2017) %>%
             pull(ave_h1b_deny_rate) + 0.005,
           label = "No Immigration\nLobbying\nin 2017", 
           color = "black", 
           size = 5.5)

# save
pdf(file = paste(MAIN_DIR, "Figure-6.pdf", sep = "/"),
    width = 15, height = 7)
print(p.ave.denial.h1b)
dev.off() 


## Appendix Figure D.1 ---------------------------------------------------------
# aggregate
tot.pet.trend <- data.merge %>% 
  select(bvd_id, year, lob_img_2017, h1b_pet_firm_year_tot) %>%
  group_by(year, lob_img_2017) %>%
  summarize(tot_h1b_pet_group = sum(h1b_pet_firm_year_tot),
            ln_tot_h1b_pet_group = log(tot_h1b_pet_group)) %>%
  ungroup() %>%
  filter(year >= 1992)

# plot
axis.title.size <- 16
colors <- c("black", "black")

p.tot.pet.trend <- ggplot(tot.pet.trend,
                          aes(x = year, 
                              y = ln_tot_h1b_pet_group,
                              group = factor(lob_img_2017),
                              color = factor(lob_img_2017),
                              shape = factor(lob_img_2017))) +
  geom_line(aes(linetype = factor(lob_img_2017), 
                color = factor(lob_img_2017))) +
  scale_linetype_manual(values = c("dashed", "solid")) +
  scale_color_manual(values = colors) +
  geom_vline(xintercept = 2016.5, 
             color = "red") +
  geom_point(size = 2) +
  scale_shape_manual(values = c(17, 19))+
  scale_x_continuous("",
                     breaks = seq(1993, 2017, by = 2), 
                     labels = seq(1993, 2017, by = 2),
                     limits = c(1991.5, 2017.5)) + 
  scale_y_continuous("Total H-1B Petitions (log)",
                     breaks = seq(5, 14, by = 2),
                     limits = c(4, 14)) +
  ggtitle("") +
  theme_bw()  +
  theme(plot.title = element_text(size = axis.title.size + 6,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 4,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 2,
                                    margin = margin(20, 0, 0, 0)),
        axis.text.x = element_text(size = axis.title.size - 4,
                                   angle = 0),
        axis.text.y = element_text(size = axis.title.size - 4),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none") + 
  annotate("text", 
           x = 2013.5, 
           y = tot.pet.trend %>% 
             filter(lob_img_2017 == 1 & year == 2013) %>%
             pull(ln_tot_h1b_pet_group) - 1.2,
           label = "Lobbied\nImmigration\nin 2017", 
           color = "black",
           size = 3) + 
  annotate("text", 
           x = 2013.5, 
           y = tot.pet.trend %>% 
             filter(lob_img_2017 == 0 & year == 2013) %>%
             pull(ln_tot_h1b_pet_group) + 1.2,
           label = "No Immigration\nLobbying\nin 2017", 
           color = "black", 
           size = 3) 

# save
pdf(file = paste(MAIN_DIR, "Appendix-Figure-D1.pdf", sep = "/"),
    width = 7, height = 5.5)
print(p.tot.pet.trend)
dev.off() 

